//
//  OuterPopulationViewController.m
//  Utils
//
//  Created by enghou on 2018/4/25.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "OuterPopulationViewController.h"
#import "NSMutableDictionary+SetContent.h"
#import "LocalPersistent.h"
@interface OuterPopulationViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>



@property(nonatomic,strong)UITextField *birthField;
@property(nonatomic,strong)UITextField *arriveField;
@property(nonatomic,strong)UITextField *leaveField;
@property(nonatomic,strong)UITextField *beginField;
@property(nonatomic,strong)UITextField *endField;
@property(nonatomic,strong)UIDatePicker *datePicker;

@end

@implementation OuterPopulationViewController

-(NSString *)orgId{
    return [[LocalPersistent sharedInstance]contentForKey:@"orgId"];
}

-(NSMutableDictionary *)param{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    BOOL result = [param setContent:self.ids forKey:@"overseaPersonnel.id" conentNeeded:NO];
    if (!result) {
        return nil;
    }
    result = [param setContent:self.foreignNameField.text forKey:@"overseaPersonnel.foreignName" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请输入外文姓!"];
        return nil;
    }
    result = [param setContent:self.foreignNamesField.text forKey:@"overseaPersonnel.englishName" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请输入外文名!"];
        return nil;
    }
    result = [param setContent:self.sexModel.ids forKey:@"overseaPersonnel.gender.id" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请选择性别"];
        return nil;
    }
    result = [param setContent:self.birthdayField.text forKey:@"overseaPersonnel.birthday" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请选择出生日期!"];
        return nil;
    }
    result = [param setContent:self.cardModel.ids forKey:@"overseaPersonnel.certificateType.id" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请选择证件类型!"];
        return nil;
    }
    result = [param setContent:self.cardNumField.text forKey:@"overseaPersonnel.certificateNo" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请输入证件号码!"];
        return nil;
    }
    result = [param setContent:self.nowProvince.code forKey:@"overseaPersonnel.province" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请选择现住地!"];
        return nil;
    }
    result = [param setContent:self.nowCity.code forKey:@"overseaPersonnel.city" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请选择现住地!"];
        return nil;
    }
    result = [param setContent:self.nowCountry.code forKey:@"overseaPersonnel.district" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请选择现住地!"];
        return nil;
    }
    if (!self.menlouYesBtn.isSelected && !self.menlouNoBtn.isSelected) {
        [self alertSome:@"请选择有无现住门楼地址!"];
        return nil;
    }
    if (self.menlouYesBtn.isSelected) {
        [param setObject:@"1" forKey:@"overseaPersonnel.isHaveHouse"];
    }else{
        [param setObject:@"0" forKey:@"overseaPersonnel.isHaveHouse"];
    }
    result = [param setContent:self.nationModel.ids forKey:@"overseaPersonnel.nationality.id" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请选择国籍!"];
        return nil;
    }
    result = [param setContent:self.proposeModel.ids forKey:@"overseaPersonnel.inflowReason.id" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请选择来华目的!"];
        return nil;
    }
    if ([self.phoneNumberField.text length] == 0 && [self.stablePhoneField.text length] == 0) {
        [self alertSome:@"请输入联系方式"];
        return nil;
    }
    result = [param setContent:self.phoneNumberField.text forKey:@"overseaPersonnel.mobileNumber" conentNeeded:NO];
    result = [param setContent:self.stablePhoneField.text forKey:@"overseaPersonnel.telephone" conentNeeded:NO];
    result = [param setContent:self.arriveDateField.text forKey:@"overseaPersonnel.arrivalTime" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请选择到达日期!"];
        return nil;
    }
    result = [param setContent:self.leaveDateField.text forKey:@"overseaPersonnel.leaveTime" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请选择离开日期!"];
        return nil;
    }
//    overseaPersonnel.name
    result = [param setContent:self.chineseNameField.text forKey:@"overseaPersonnel.name" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请输入中文名字!"];
        return nil;
    }
//    overseaPersonnel.certificateStrartDay
    result = [param setContent:self.cardBeginField.text forKey:@"overseaPersonnel.certificateStrartDay" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请选择证件有效期!"];
        return nil;
    }
    result = [param setContent:self.cardEndField.text forKey:@"overseaPersonnel.certificateEndDay" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请选择证件有效期!"];
        return nil;
    }
    if (!self.isImportantBtn.isSelected && !self.noImportantBtn.isSelected) {
        [self alertSome:@"请选择是否重点关注人员!"];
        return nil;
    }
    if (self.isImportantBtn.isSelected) {
        [param setObject:@"1" forKey:@"overseaPersonnel.isEmphasis"];
    }else{
        [param setObject:@"0" forKey:@"overseaPersonnel.isEmphasis"];
    }
    result = [param setContent:self.occupyCategoryModel.ids forKey:@"overseaPersonnel.profession.id" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请选择职业类别!"];
        return nil;
    }
    result = [param setContent:self.occupyField.text forKey:@"overseaPersonnel.careerText" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请输入职业!"];
        return nil;
    }
    result = [param setContent:self.religionModel.ids forKey:@"overseaPersonnel.faith.id" conentNeeded:NO];
    [param setContent:self.serviceLocateField.text forKey:@"overseaPersonnel.workUnit" conentNeeded:NO];
    return param;
}

-(void)postWithPath:(NSString *)path param:(NSDictionary *)param{
//    @"/mobile/comprehensiveGovernance/overSeaPerson/updateOverSeaPerson.action"
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:path parameter:param completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            if ([self.theDelete respondsToSelector:@selector(beginRefresh)]) {
                [self.theDelete performSelector:@selector(beginRefresh)];
                [self pop];
            }
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(void)updateWithFile:(NSString *)file{
    NSMutableDictionary *param = [self param];
    if (param) {
        if ([file length] > 0) {
            [param addEntriesFromDictionary:@{@"file.path":file}];
        }
        [self postWithPath:@"/mobile/comprehensiveGovernance/overSeaPerson/updateOverSeaPerson.action" param:param];
    }
}

-(void)addWithFile:(NSString *)file{
    NSMutableDictionary *param = [self param];
    if (param) {
        if ([file length] > 0) {
            [param addEntriesFromDictionary:@{@"file.path":file}];
        }
        [self postWithPath:@"/mobile/comprehensiveGovernance/overSeaPerson/addOverSeaPerson.action" param:param];
    }
}

-(void)submitAction{
    if (self.ids) {
        UIImage *image = self.headImageView.image;
        if (image) {
            NSData *data = UIImageJPEGRepresentation(image, 0.1);
            [self uploadImage:data successBlock:^(NSString *filepath) {
                [self updateWithFile:filepath];
            }];
        }else{
            //直接修改
            [self updateWithFile:nil];
        }
    }else{
        //新增
        UIImage *image = self.headImageView.image;
        if (image) {
            NSData *data = UIImageJPEGRepresentation(image, 0.1);
            [self uploadImage:data successBlock:^(NSString *filepath) {
                [self addWithFile:filepath];
            }];
        }else{
            //直接修改
            [self addWithFile:nil];
        }
    }
}

-(void)deleAction{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/overSeaPerson/deleteOverSeaPersonById.action" parameter:@{@"id":self.ids} completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            [self.theDelete performSelector:@selector(beginRefresh)];
            [self pop];
        }else{
            [self alertSome:kNetWorkFail];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(void)uploadImage:(NSData *)image successBlock:(void(^)(NSString *))suc{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]uploadImageWithPath:kUploadFilePath parameter:nil files:@[image] fileName:@"file" suffix:@"jpg" completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source listItemClass:NSClassFromString(@"FileItemModel")];
        if (model.success) {
            FileItemModel *file = model.list[0];
            suc(file.path);
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}
//-(BOOL)setContent:(NSString *)content forKey:(NSString *)key param:(NSDictionary *)param{
//
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *title;
    if (self.ids) {
        title = @"保存";
    }else{
        title = @"新增";
    }
    UIBarButtonItem *edit = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(submitAction)];
    edit.tintColor = [UIColor whiteColor];
    if (self.ids) {
        UIBarButtonItem *delete = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleAction)];
        delete.tintColor = [UIColor whiteColor];
        self.navigationItem.rightBarButtonItems = @[delete,edit];
    }else{
        self.navigationItem.rightBarButtonItem = edit;
    }
    
    
    self.sexModel = [[PropertyDictItemModel alloc]init];
    self.cardModel = [[PropertyDictItemModel alloc]init];
    self.nationModel = [[PropertyDictItemModel alloc]init];
    self.proposeModel = [[PropertyDictItemModel alloc]init];
    self.occupyCategoryModel = [[PropertyDictItemModel alloc]init];
    self.religionModel = [[PropertyDictItemModel alloc]init];
    
    self.nowProvince = [[Province alloc]init];
    self.nowCity = [[City alloc]init];
    self.nowCountry = [[Country alloc]init];
    
    self.provinces = [ProvincesGenerator generatProvinces];
    
    self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 240)];
    self.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    self.datePicker.backgroundColor = [UIColor whiteColor];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    self.datePicker.minimumDate = [NSDate date];
    
    self.birthField = [self textField];
    self.arriveField = [self textField];
    self.leaveField = [self textField];
    self.beginField = [self textField];
    self.endField = [self textField];
    
    
    
    self.provincePicker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 240)];
    self.provincePicker.backgroundColor = [UIColor whiteColor];
    self.provincePicker.delegate = self;
    self.provincePicker.dataSource = self;
    self.provinceField = [[UITextField alloc]init];
    [self.view addSubview:self.provinceField];
    self.provinceField.inputView = self.provincePicker;
    self.provinceField.inputAccessoryView = [self inputAccessView];
    
    if (self.ids) {
        [self loadData];
    }
}

-(UITextField *)textField{
    UITextField *field = [[UITextField alloc]init];
    [self.view addSubview:field];
    field.inputView = self.datePicker;
    field.inputAccessoryView = [self inputAccessView];
    return field;
}

-(void)loadData{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:kGetOuterDetail parameter:@{@"id":self.ids} completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            NSDictionary *data = [model.data objectForKey:@"data"];
            NSString *imgUrl = [data objectForKey:@"imgUrl"];
            if ([[imgUrl substringToIndex:4] isEqualToString:@"http"]) {
                [self.headImageView sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"icon_empty_head"]];
            }else{
                NSString *path = [NSString stringWithFormat:@"%@/%@",ServerPath,imgUrl];
                [self.headImageView sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"icon_empty_head"]];
            }
            self.foreignNameField.text = data[@"foreignName"];
            self.foreignNamesField.text = data[@"englishName"];
            self.sexField.text = data[@"gender"][@"displayName"];
            self.sexModel.ids = [model contentForKey:@"id" withSource:data[@"gender"]];
            self.chineseNameField.text = data[@"name"];
            self.leaveDateField.text = data[@"leaveTime"];
            self.arriveDateField.text = data[@"arrivalTime"];
            self.birthdayField.text = data[@"birthday"];
            self.occupyField.text = data[@"careerText"];
            self.cardBeginField.text = data[@"certificateStrartDay"];
            self.cardEndField.text = data[@"certificateEndDay"];
            self.cardNumField.text = data[@"certificateNo"];
            self.cardTypeField.text = data[@"certificateType"][@"displayName"];
            self.cardModel.ids = [model contentForKey:@"id" withSource:data[@"certificateType"]];
            self.locateAddressField.text = data[@"address"];
            BOOL hasHouse = [data[@"isHaveHouse"] boolValue];
            if (hasHouse) {
                self.menlouYesBtn.selected = YES;
                self.nowLocateDetailField.text = data[@"currentAddress"];
                NSMutableAttributedString *target = [[NSMutableAttributedString alloc]initWithString:@"*现住门(楼)详址" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:15.f]}];
                self.nowLocateDetailField.placeholder = @"请输入门楼详址";
                [target addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:17.f]} range:NSMakeRange(0, 1)];
                self.changeState.attributedText = target;
            }else{
                self.menlouNoBtn.selected = YES;
                self.nowLocateDetailField.text = data[@"noHouseReason"];
                NSMutableAttributedString *target = [[NSMutableAttributedString alloc]initWithString:@"*无住房原因" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:17]}];
                [target addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:17.f]} range:NSMakeRange(0, 1)];
                self.changeState.attributedText = target;
            }
            _nationModel.ids = [model contentForKey:@"id" withSource:data[@"nationality"]];
            self.nationField.text = data[@"nationality"][@"displayName"];
            self.proposeModel.ids = [model contentForKey:@"id" withSource:data[@"inflowReason"]];
            self.supposeField.text = [model contentForKey:@"displayName" withSource:data[@"inflowReason"]];
            BOOL isEmphasis = [[data objectForKey:@"isEmphasis"] boolValue];
            if (isEmphasis) {
                _isImportantBtn.selected = YES;
            }else{
                _noImportantBtn.selected = NO;
            }
            self.stablePhoneField.text = data[@"telephone"];
            self.phoneNumberField.text = data[@"mobileNumber"];
            self.serviceLocateField.text = data[@"workUnit"];
            self.occupyCategoryModel.ids = [model contentForKey:@"id" withSource:data[@"profession"]];
            self.occupyCategoryField.text = [model contentForKey:@"displayName" withSource:data[@"profession"]];
            self.nowProvince.code = [model contentForKey:@"province" withSource:data];
            self.nowCity.code = [model contentForKey:@"city" withSource:data];
            self.nowCountry.code = [model contentForKey:@"district" withSource:data];
            self.zongjiaoField.text = data[@"faith"][@"displayName"];
            self.religionModel.ids = data[@"faith"][@"id"];
            self.orgId = [model contentForKey:@"id" withSource:data[@"organization"]];
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

#pragma mark - 选择证件类型
-(void)chooseCardType{
    if (self.cardTypes) {
        [self alertWithArray:self.cardTypes choosenBock:^(PropertyDictItemModel *model) {
            self.cardModel = model;
            self.cardTypeField.text = self.cardModel.displayName;
        }];
    }else{
        [self commonAlertWithContent:@"证件代码" choosenBlock:^(PropertyDictItemModel *model) {
            self.cardModel = model;
            self.cardTypeField.text = self.cardModel.displayName;
        } getList:^(NSArray *array) {
            self.cardTypes = array;
        }];
    }
}
#pragma mark 选择国籍
//国籍
-(void)chooseNation{
    if (self.nations) {
        [self alertWithArray:self.nations choosenBock:^(PropertyDictItemModel *model) {
            self.nationModel = model;
            self.nationField.text = model.displayName;
        }];
    }else{
        [self commonAlertWithContent:@"国籍" choosenBlock:^(PropertyDictItemModel *model) {
            self.nationModel = model;
            self.nationField.text = model.displayName;
        } getList:^(NSArray *array) {
            self.nations = array;
        }];
    }
}

#pragma mark 选择性别
-(void)chooseSex{
    if (self.sexArray) {
        [self alertWithArray:self.sexArray choosenBock:^(PropertyDictItemModel *model) {
            self.sexModel = model;
            self.sexField.text = model.displayName;
        }];
    }else{
        [self commonAlertWithContent:@"性别" choosenBlock:^(PropertyDictItemModel *model) {
            self.sexModel = model;
            self.sexField.text = model.displayName;
        } getList:^(NSArray *array) {
            self.sexArray = array;
        }];
    }
}



#pragma mark - 选择职业类别
-(void)chooseOccupyCategory{
    if (self.occupyCategorys) {
        [self alertWithArray:self.occupyCategorys choosenBock:^(PropertyDictItemModel *model) {
            self.occupyCategoryModel = model;
            self.occupyCategoryField.text = model.displayName;
        }];
    }else{
        [self commonAlertWithContent:@"职业" choosenBlock:^(PropertyDictItemModel *model) {
            self.occupyCategoryModel = model;
            self.occupyCategoryField.text = model.displayName;
        } getList:^(NSArray *array) {
            self.occupyCategorys = array;
        }];
    }
}

#pragma mark - 选择宗教信仰
-(void)chooseReligon{
    if (self.religions) {
        [self alertWithArray:self.religions choosenBock:^(PropertyDictItemModel *model) {
            self.religionModel = model;
            self.zongjiaoField.text = model.displayName;
        }];
    }else{
        [self commonAlertWithContent:@"宗教信仰" choosenBlock:^(PropertyDictItemModel *model) {
            self.religionModel = model;
            self.zongjiaoField.text = model.displayName;
        } getList:^(NSArray *array) {
            self.religions = array;
        }];
    }
}

-(void)cancelAction{
    [self.view endEditing:YES];
}

-(void)sureAction{
    NSString *date = [[self.datePicker date]dateLineStrOnlyDay];
    if ([self.birthField isFirstResponder]) {
        self.birthdayField.text = date;
    }
    if ([self.arriveField isFirstResponder]) {
        self.arriveDateField.text = date;
    }
    if ([self.leaveField isFirstResponder]) {
        self.leaveDateField.text = date;
    }
    if ([self.beginField isFirstResponder]) {
        self.cardBeginField.text = date;
    }
    if ([self.endField isFirstResponder]) {
        self.cardEndField.text = date;
    }
    if ([self.provinceField isFirstResponder]) {
        NSInteger provinceIdx = [_provincePicker selectedRowInComponent:0];
        NSInteger cityIdx = [_provincePicker selectedRowInComponent:1];
        NSInteger countryIdx = [_provincePicker selectedRowInComponent:2];
        _nowProvince = _provinces[provinceIdx];
        _nowCity = _nowProvince.citys[cityIdx];
        _nowCountry = _nowCity.countrys[countryIdx];
        self.locateAddressField.text = [NSString stringWithFormat:@"%@%@%@",_nowProvince.name,_nowCity.name,_nowCountry.name];
    }
    [self.view endEditing:YES];
}




-(void)choosePopose{
    if (self.proposes) {
        [self alertWithArray:self.proposes choosenBock:^(PropertyDictItemModel *model) {
            self.proposeModel = model;
            self.supposeField.text = model.displayName;
        }];
    }else{
        [self commonAlertWithContent:@"来华目的" choosenBlock:^(PropertyDictItemModel *model) {
            self.proposeModel = model;
            self.supposeField.text = model.displayName;
        } getList:^(NSArray *array) {
            self.proposes = array;
        }];
    }
}

-(void)showImagePicker{
    TZImagePickerController *picker = [[TZImagePickerController alloc]initWithMaxImagesCount:1 delegate:nil];
    picker.didFinishPickingPhotosHandle = ^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        UIImage *image = photos[0];
        self.headImageView.image = image;
    };
    [self presentViewController:picker animated:YES completion:nil];
}

-(void)commonAlertWithContent:(NSString *)content choosenBlock:(void(^)(PropertyDictItemModel *))choosen getList:(void(^)(NSArray *))getList{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]getPropertyDictWithDomainName:content successBlock:^(BaseModel *model) {
        [self endLoading];
        if (model.success) {
            getList(model.list);
            [self alertWithArray:model.list choosenBock:choosen];
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(void)alertWithArray:(NSArray <PropertyDictItemModel *>*)array choosenBock:(void(^)(PropertyDictItemModel *))choosen{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    for (PropertyDictItemModel *model in array) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:model.displayName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            choosen(model);
        }];
        [alert addAction:action];
    }
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        [self showImagePicker];
    }else if (indexPath.row == 3){
        [self chooseSex];
    }else if (indexPath.row == 5){
        [self chooseCardType];
    }else if (indexPath.row == 7){
        [self.provinceField becomeFirstResponder];
    }else if (indexPath.row == 10){
        [self chooseNation];
    }else if (indexPath.row == 11){
        [self choosePopose];
    }else if (indexPath.row == 18){
        [self chooseOccupyCategory];
    }else if (indexPath.row == 20){
        [self chooseReligon];
    }
}

- (IBAction)menlouYesAction:(id)sender {
    self.menlouYesBtn.selected = YES;
    self.menlouNoBtn.selected = NO;
    NSMutableAttributedString *target = [[NSMutableAttributedString alloc]initWithString:@"*现住门(楼)详址" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:15.f]}];
    self.nowLocateDetailField.placeholder = @"请输入门楼详址";
    [target addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:17.f]} range:NSMakeRange(0, 1)];
    self.changeState.attributedText = target;
}
- (IBAction)menlouNoAction:(id)sender {
    self.menlouYesBtn.selected = NO;
    self.menlouNoBtn.selected = YES;
    NSMutableAttributedString *target = [[NSMutableAttributedString alloc]initWithString:@"*无住房原因" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:17]}];
    [target addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:17.f]} range:NSMakeRange(0, 1)];
    self.changeState.attributedText = target;
}
- (IBAction)leaveAction:(id)sender {
    [self.leaveField becomeFirstResponder];
}

- (IBAction)arriveAction:(id)sender {
    [self.arriveField becomeFirstResponder];
}

- (IBAction)isImportantAction:(id)sender {
    self.isImportantBtn.enabled = YES;
    self.noImportantBtn.enabled = NO;
}

- (IBAction)showCardBeginDate:(id)sender {
    [self.beginField becomeFirstResponder];
}

- (IBAction)noImportantAction:(id)sender {
    self.isImportantBtn.enabled = NO;
    self.noImportantBtn.enabled = YES;
}
- (IBAction)showCardEndDateAction:(id)sender {
    [self.endField becomeFirstResponder];
}

#pragma mark - UIPickerViewDelegate And DataSource
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return [self.provinces count];
    }else if (component == 1){
        Province *province = [self.provinces objectAtIndex:[pickerView selectedRowInComponent:0]];
        return [province.citys count];
    }else if (component == 2){
        Province *province = [self.provinces objectAtIndex:[pickerView selectedRowInComponent:0]];
        City *city = [province.citys objectAtIndex:[pickerView selectedRowInComponent:1]];
        return [city.countrys count];
    }else{
        return 0;
    }
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *label = [[UILabel alloc]init];
    label.textColor = [UIColor colorWithHexString:@"333333"];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:14];
    if (component == 0) {
        Province *province = [self.provinces objectAtIndex:row];
        label.text = province.name;
    }else if (component == 1){
        Province *province = [self.provinces objectAtIndex:[pickerView selectedRowInComponent:0]];
        City *city = [province.citys objectAtIndex:component];
        label.text =  city.name;
    }else if (component == 2){
        Province *province = [self.provinces objectAtIndex:[pickerView selectedRowInComponent:0]];
        City *city = [province.citys objectAtIndex:[pickerView selectedRowInComponent:1]];
        Country *c = [city.countrys objectAtIndex:component];
        label.text = c.name;
    }
    return label;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        [pickerView selectRow:0 inComponent:1 animated:NO];
        [pickerView selectRow:0 inComponent:2 animated:NO];
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
    }else if (component == 1){
        [pickerView selectRow:0 inComponent:2 animated:NO];
        [pickerView reloadComponent:2];
    }
}
- (IBAction)showBirthDayAction:(id)sender {
    [self.birthField becomeFirstResponder];
}
@end
